package com.smh.demo1.a35;

import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author shiminghui
 * @date 2025/5/9 15:54
 * @description: TODO
 */
public class TestAopAutoConfiguration {
    public static void main(String[] args) {

        GenericApplicationContext context = new GenericApplicationContext();
        // 注册一些BeanFactory和Bean后处理器
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context);

        // 测试AopAutoConfiguration里面注解的效果
        StandardEnvironment environment = new StandardEnvironment();
//        environment.getPropertySources().addFirst(new SimpleCommandLinePropertySource("--spring.aop.auto=false"));
        context.setEnvironment(environment);

        context.registerBean("config", Config.class);
        context.refresh();

        // 打印一下Bean
        System.out.println("======================================");
        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }

        // 检测一下是不是CGlib代理
        AnnotationAwareAspectJAutoProxyCreator bean = context.getBean(AnnotationAwareAspectJAutoProxyCreator.class);
        System.out.println(bean.isProxyTargetClass());


    }

    // 直接使用@bean注解创建配置类会导致条件注解失效
    @Configuration
    @Import(MyImportSelector.class)
    static class Config {
    }

    static class MyImportSelector implements DeferredImportSelector {
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            return new String[]{"org.springframework.boot.autoconfigure.aop.AopAutoConfiguration"};
        }
    }
}
